<script>
// 回帖页面（展开评论）
import { getRepliesData } from "../api/details";
import { reactive, toRefs } from "@vue/reactivity";
import { onMounted } from "@vue/runtime-core";
import { useRouter } from "vue-router";
import { ref } from "vue";
import { Toast } from "vant";
export default {
  setup() {
    const list = reactive({
      replies: null, //贴主评论
      replyLists: null, //底下评论
      commens: "",
      gettime: "",
      // lists:null
      commenslist: [],
      emid: "",
      groupId: "",
      Id: "",

    });

    const router = useRouter();
    list.emid = router.currentRoute._value.query.id;
    list.groupId = router.currentRoute._value.query.groupId;

    onMounted(() => {
      // 书评
      getRepliesData(list.emid, list.groupId).then((data) => {
        console.log(data, "45757445");
        list.replies = data.data.threadInfo;
        console.log(list.replies, "sss");

        list.replyLists = data.data.replyLists;
        console.log(list.replyLists);

        let pinlist = JSON.parse(localStorage.getItem("commens"));
        console.log(pinlist, "111");
        if (pinlist && pinlist.length) {
          pinlist.forEach((item) => {
            if (item.emid == list.emid) {
              list.commenslist.push(item);
            } else {
              list.commenslist;
            }
          });
        } else {
          list.commenslist;
        }
      });
    });
    // 回复评论弹框
    const show = ref(false);
    const showPopup = () => {
      show.value = true;
    };
    let bok = JSON.parse(localStorage.getItem("booklD"));
    list.Id = bok;
    // 回复评论

    let submit = () => {
      // 发布日期
      console.log(list.replies, "楼主");
      if (list.commens == "") {
        Toast.fail("请输入内容");
        return;
      } else {
        let mm =
          new Date().getMonth() > 9
            ? new Date().getMonth() + 1
            : new Date().getMonth() == 9
            ? new Date().getMonth() + 1
            : "0" + (new Date().getMonth() + 1);
        let dd =
          new Date().getDate() < 10
            ? "0" + new Date().getDate()
            : new Date().getDate();
        list.gettime = mm + "-" + dd;
        console.log("回复", list.gettime, list.commens);
        let commen = [];
        // const id = 0;

        commen.unshift({
          emid: list.emid,
          groupId: list.groupId,
          name: list.replies.userInfo.nickname,
          // id: id + 1,
          nickname: "熊猫友" + list.Id,
          avatarUrl:
            "http://cdn.static.17k.com/user/avatar/08/28/06/73840628.jpg-88x88?v=1654241197000",
          text: list.commens,
          time: list.gettime,
          vipLevel: "",
          comment: 0,
          spot: 0,
        });
        let pinlist = JSON.parse(localStorage.getItem("commens"));
        if (pinlist && pinlist.length) {
          pinlist.push(commen[0]);
        } else {
          pinlist = [];
          pinlist.push(commen[0]);
        }
          list.commenslist.push(commen[0]);
        localStorage.setItem("commens", JSON.stringify(pinlist));

        show.value = false;

        list.commens = "";
      }
    };
    
    // 评论
    return {
      ...toRefs(list),
      show,
      showPopup,
      submit,
    };
  },

};
</script>


<template>
  <div class="replies">


    <div class="top_navbar">
      <div class="back_l">
        <span
          @click="$router.go(-1)"
          class="iconfont icon-zuojiantou-copy"
        ></span>
      </div>
      <p>贴子详情</p>
      <div class="search_r">
        <!-- <span class="iconfont icon-bangzhu"></span> -->
      </div>
    </div>

    <div class="replies_box">
      <div class="post_owner">
        <div class="comments" v-if="replies">
          <div class="user_box">
            <div class="avatar">
              <img :src="replies.userInfo.avatarUrl" alt="" />
            </div>
            <div class="name">
              <p>{{ replies.userInfo.nickname }}</p>
              <div>
                <span>VIP{{ replies.userInfo.vipLevel }}</span>
              </div>
            </div>
          </div>
          <p class="write-p">{{ replies.summary }}</p>
          <p class="time">发布于2021-12-01</p>
          <div class="reply" is-link @click="showPopup">回复</div>
          <van-popup
            v-model:show="show"
            position="bottom"
            :style="{ height: '24%' }"
          >
            <div class="user_box">
              <div class="avatar">
                <img :src="replies.userInfo.avatarUrl" alt="" />
              </div>
              <div class="name">
                <p>{{ replies.userInfo.nickname }}</p>
                <div>
                  <span>VIP{{ replies.userInfo.vipLevel }}</span>
                </div>
              </div>
            </div>
            <p class="write">{{ replies.summary }}</p>
            <div style="display: flex; align-items: center">
              <input
                type="text"
                :placeholder="'回复: ' + replies.userInfo.nickname"
                autofocus
                v-model="commens"
              />
              <p style="padding: 0 10px; color: #00c290" @click="submit">
                发布
              </p>
            </div>
          </van-popup>
        </div>
      </div>
      <div class="bottom_comments">
        <div class="title" v-if="replyLists">
          全部评论（{{ replyLists.length + commenslist.length }}）
        </div>
        <div class="title" v-if="!replyLists">
          全部评论（{{ commenslist.length }}）
        </div>
        <main v-if="commenslist">
          <div
            class="comments_box"
            v-for="(item, index) in commenslist"
            :key="index"
          >
            <div class="left">
              <img :src="item.avatarUrl" alt="" />
            </div>
            <div class="right">
              <div class="name">
                {{ item.nickname }}
                <span v-if="item.vipLevel">VIP{{ item.vipLevel }}</span>
              </div>
              <div class="pl">{{ item.text }}</div>
              <div class="time2">{{ item.time }}</div>
            </div>
          </div>
        </main>
        <main v-if="replyLists">
          <div
            class="comments_box"
            v-for="(item, index) in replyLists"
            :key="index"
          >
            <div class="left">
              <img :src="item.userInfo.avatarUrl" alt="" />
            </div>
            <div class="right">
              <div class="name">
                {{ item.userInfo.nickname }}
                <span v-if="item.userInfo.vipLevel"
                  >VIP{{ item.userInfo.vipLevel }}</span
                >
              </div>
              <div class="pl">{{ item.content[0].text }}</div>
              <div class="time2">8天前</div>
            </div>
          </div>
        </main>
        <van-empty v-if="!replyLists" description="描述文字" />
      </div>
    </div>
  </div>
</template>


<style lang="scss" scoped>
::v-deep {
  .van-popup--bottom {
    border-radius: 10px 10px 0px 0px;
    background-color: #fff;
    padding: 10px 10px;
    box-sizing: border-box;
  }
  .van-overlay {
    background: #00000045;
  }
}
.replies {
  background-color: #f4f4f4;
  
  .top_navbar {
    position: sticky;
    top: 0;
    z-index: 10;
    padding: 0 16px;
    height: 45px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 2px solid #f2f2f2;
    background-color: #fff;
    .iconfont {
      font-size: 22px;
    }
  }
  .replies_box {
    .post_owner {
      background-color: #fff;
      .comments {
        padding: 20px 16px 10px;
        position: relative;
        // border-bottom: 1px solid #ccc;
        .reply {
          position: absolute;
          right: 20px;
          bottom: 8px;
          font-size: 12px;
          color: #525252;
        }
        .user_box {
          display: flex;
          align-items: center;

          .avatar {
            //   width: 40px;
            height: 30px;
            img {
              height: 100%;
              border-radius: 50%;
            }
          }
          .name {
            margin-left: 10px;
            display: flex;
            align-items: flex-start;
            p {
              font-size: 13px;
              //   color: #77757f;
              font-weight: 600;
              margin-bottom: 5px;
            }
            div {
              display: flex;
              align-items: center;
              margin-left: 4px;
              span {
                padding: 1.2px 4px;
                font-size: 6px;
                color: #7e5014;
                background-color: #f9d466;
                display: flex;
                border-radius: 5px 0 5px 0;
                align-items: center;
              }
              a {
                transform: scale(0.8);
                border-radius: 5px 0 5px 0;
                overflow: hidden;
                font-size: 6px;
                color: #7e5014;
                // background-color: #b4ccff;
                color: #5a78a8;
                padding-right: 4px;
                display: flex;
                b {
                  padding: 1px 2px;
                  font-size: 6px;
                  font-weight: normal;
                  margin-right: 2px;
                  color: #fff;
                  //   background-color: #87a1d2;
                }
              }
            }
          }
        }
        .write-p {
          padding: 15px 0 0;
          margin-bottom: 10px;
          font-size: 17px;
          text-indent: 1.5em;
          //   -webkit-line-clamp: 3;
          //   display: -webkit-box;
          //   -webkit-box-orient: vertical;
          //   overflow: hidden;
          //   text-overflow: ellipsis;
        }
        .write {
          padding: 3px;
          text-indent: 1.5em;
          background: #f8f4f4;
          margin-top: 10px;
          margin-bottom: 10px;
          font-size: 15px;
          -webkit-line-clamp: 2;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        input {
          padding: 10px;
          flex: 1;
          width: 92%;
          border: 1px solid #aaaaaa;
          font-size: 14px;
          border-radius: 5px;
        }
        .time {
          color: #9b9b9b;
          font-size: 12px;
        }
      }
    }
    .bottom_comments {
      margin-top: 10px;
      background-color: #fff;
      .title {
        padding: 15px 16px;
        font-size: 14px;
        font-weight: 600;
        color: #2c2c2c;
        border-bottom: 1px solid #f2f2f2;
      }
      .comments_box {
        padding: 15px 16px 8px;
        display: flex;
        border-bottom: 1px solid #f2f2f2;
        .left {
          width: 30px;
          height: 30px;
          img {
            height: 100%;
            border-radius: 50%;
          }
        }
        .right {
          margin-left: 9px;
          .name {
            font-size: 14px;
            font-weight: 600;
            color: #000000;
            display: flex;
            span {
              transform: scale(0.8);

              padding: 1.2px 4px;
              font-size: 6px;
              color: #7e5014;
              background-color: #f9d466;
              display: flex;
              border-radius: 5px 0 5px 0;
              align-items: center;
            }
          }
          .pl {
            font-size: 14px;
            color: #272727;
            padding: 8px 0;
          }
          .time2 {
            font-size: 12px;
            color: #525252;
          }
        }
      }
    }
  }
}
</style>